<?php

/**
 * 客服人员相关模块
 */
if (!defined('IN_DIALOG'))
    die("Access Denied");

$list = array();

if ($op == 'add') {
    //添加客服人员
    $username = $_POST['username'];
    $password = $_POST['password'];
    $email = $_POST['email'];
    $company = $_POST['company'];

    if (Utils::submitCheck('addsubmit')) {
        if (User::isHave($username))
            Utils::showMessage('username_is_exists', $_SGLOBAL['refer']);

        $companyinfo = $_SGLOBAL['db']->getOne('company', array('name' => $company));

        if ($companyinfo) {
            if (User::addUser($username, $password, $email, 0, $companyinfo['cid'])) {
                Utils::showMessage('do_success', $_SGLOBAL['refer']);
            }else
                Utils::showMessage('do_error', $_SGLOBAL['refer']);
        }else {
            Utils::showMessage('company_not_exists', $_SGLOBAL['refer']);
        }
    }
    $_SGLOBAL['view']->display('setup_support_add');
} elseif ($op == 'list') {
    //列表
    $sql = "SELECT m.*,g.title AS grouptitle FROM " . Utils::tname('member') . ' m';
    $sql.=' LEFT JOIN ' . Utils::tname('usergroup') . ' g ON m.gid=g.gid';
    $sql.=' LEFT JOIN ' . Utils::tname('memberfields') . ' mf ON m.uid=mf.uid';
    $query = $_SGLOBAL['db']->query($sql);
    if ($query) {
        while ($row = $_SGLOBAL['db']->fetchArray($query)) {
            $row['lastupdate'] = Utils::sDate($row['lastupdate']);
            $row['dateline'] = Utils::sDate($row['dateline']);
            $list[$row['uid']] = $row;
        }
        $sql = 'SELECT c.cid,mf.cadmin,c.name AS cname,mf.uid FROM ' . Utils::tname('company') . ' AS c,' . Utils::tname('memberfields') . ' AS mf WHERE c.cid=mf.cid AND mf.uid IN(' . implode(',', array_keys($list)) . ')';
        $query = $_SGLOBAL['db']->query($sql);
        while ($row = $_SGLOBAL['db']->fetchArray($query)) {
            $list[$row['uid']] = array_merge($list[$row['uid']], $row);
        }
    }
    $_SGLOBAL['view']->assign('memberlist', $list);
    $_SGLOBAL['view']->display('setup_support_list');
} elseif ($op == 'search') {
    if (Utils::submitCheck('searchsubmit')) {
        //搜索
        $username = trim($_GET['username']);

        $sql = "SELECT m.*,g.title AS grouptitle FROM " . Utils::tname('member') . ' m';
        $sql.=' LEFT JOIN ' . Utils::tname('usergroup') . ' g ON m.gid=g.gid';
        $sql.=' LEFT JOIN ' . Utils::tname('memberfields') . ' mf ON m.uid=mf.uid';

        if(!empty($username))
            $sql.=" WHERE m.username='$username'";

        $query = $_SGLOBAL['db']->query($sql);
        if ($query) {
            while ($row = $_SGLOBAL['db']->fetchArray($query)) {
                $row['lastupdate'] = Utils::sDate($row['lastupdate']);
                $row['dateline'] = Utils::sDate($row['dateline']);
                $list[$row['uid']] = $row;
            }
            
            $sql = 'SELECT c.cid,mf.cadmin,c.name AS cname,mf.uid FROM ' . Utils::tname('company') . ' AS c,' . Utils::tname('memberfields') . ' AS mf WHERE c.cid=mf.cid AND mf.uid AND mf.uid IN(' . implode(',', array_keys($list)) . ')';
            $query = $_SGLOBAL['db']->query($sql);
            while ($row = $_SGLOBAL['db']->fetchArray($query)) {
                $list[$row['uid']] = array_merge($list[$row['uid']], $row);
            }
        }
    }
    $_SGLOBAL['view']->assign('memberlist', $list);
    $_SGLOBAL['view']->display('setup_support_search');
} else {
    $_SGLOBAL['view']->display('setup_support');
}
?>